/**
 * 
 */
package es.ats.codington.festival.services;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import es.ats.codington.festival.daos.ManagerDAO;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.domain.People;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.exceptions.DAOException;
import es.ats.codington.festival.exceptions.ServiceException;
import es.ats.codington.festival.interfaces.daos.IEventSignUpDAO;
import es.ats.codington.festival.interfaces.daos.IVisitorDAO;
import es.ats.codington.festival.interfaces.services.IVisitorService;


/**
 * @author Codington_RED
 *
 */
public class VisitorService implements IVisitorService {
	
	/**
	 * method that obtains the ID from a Person 
	 * @param people
	 * @return idPeople
	 * @throws ServiceException
	 */
	@Override
	public int getIdPeople(People people) throws ServiceException {
		
		int idPeople = 0;
		ManagerDAO manager = null;
		IVisitorDAO visitorDAO = null;
		
		try {
			manager = new ManagerDAO();
			visitorDAO = manager.getVisitorDAO();
			idPeople = visitorDAO.getIdPeople(people);
			
			manager.closeConnection();
			
		} catch (DAOException ex){
			if(manager != null){
				manager.closeError();
			}
			
			throw new ServiceException("Error accessing database", ex);
			
		}
		
		
		return idPeople;
	}
	
	/**
	 * method that obtains the ID from a visitor
	 * @param visitor
	 * @return idVisitor
	 * @throws ServiceException
	 */
	@Override
	public int getIdVisitor(Visitor visitor) throws ServiceException {
		
		int idVisitor = 0;
		ManagerDAO manager = null;
		IVisitorDAO visitorDAO = null;
		
		try {
			manager = new ManagerDAO();
			visitorDAO = manager.getVisitorDAO();
			idVisitor = visitorDAO.getIdVisitor(visitor);
			
			manager.closeConnection();
			
		} catch (DAOException ex){
			if(manager != null){
				manager.closeError();
			}
			
			throw new ServiceException("Error accessing database", ex);
			
		}
		
		
		return idVisitor;
	}
	
	
	/**
	 * method that checks if a visitor is an admin or not
	 * @param visitor
	 * @return isAdmin
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public boolean checkAdmin(Visitor visitor) throws ServiceException, SQLException {
		boolean isAdmin = false;
		ManagerDAO manager = null;
		IVisitorDAO visitorDAO = null;
		
		try {
			manager = new ManagerDAO();
			visitorDAO = manager.getVisitorDAO();
			isAdmin = visitorDAO.getCheckAdmin(visitor);
			
			manager.closeConnection();
			
		} catch (DAOException ex){
			if(manager != null){
				manager.closeError();
			}
			
			throw new ServiceException("Error accessing database", ex);
			
		}
		
		
		return isAdmin;
	}
	
	
	/**
	 * method that checks if a visitor have logged rightly 
	 * @param visitor
	 * @return isLogin
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public boolean checkLogin(Visitor visitor) throws ServiceException, SQLException {
		boolean isLogin = false;
		ManagerDAO manager = null;
		IVisitorDAO visitorDAO = null;
		
		try {
			manager = new ManagerDAO();
			visitorDAO = manager.getVisitorDAO();
			isLogin = visitorDAO.getCheckLogin(visitor);
			
			manager.closeConnection();
			
		} catch (DAOException ex){
			if(manager != null){
				manager.closeError();
			}
			
			throw new ServiceException("Error accessing database", ex);
			
		}
		
		
		return isLogin;
	}
	
	
	/**
	 * method that checks if a people have been logged
	 * @param people
	 * @return isLogin
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public boolean checkPeople(People people) throws ServiceException, SQLException {
		boolean isLogin = false;
		ManagerDAO manager = null;
		IVisitorDAO visitorDAO = null;
		
		try {
			manager = new ManagerDAO();
			visitorDAO = manager.getVisitorDAO();
			isLogin = visitorDAO.getCheckPeople(people);
			
			manager.closeConnection();
			
		} catch (DAOException ex){
			if(manager != null){
				manager.closeError();
			}
			
			throw new ServiceException("Error accessing database", ex);
			
		}
		
		
		return isLogin;
	}
	
	
	/**
	 * method that creates a visitor
	 * @param people
	 * @return isCreated
	 * @throws ServiceException
	 */
	@Override
	public boolean createVisitor(People people) throws ServiceException{
		
		boolean isCreated = false;
		ManagerDAO manager = null;
		IVisitorDAO visitorDAO = null;
		
		try {
			manager = new ManagerDAO();
			visitorDAO = manager.getVisitorDAO();
			isCreated = visitorDAO.insertData(people);
			
			manager.closeConnection();
			
		} catch (DAOException ex){
			if(manager != null){
				manager.closeError();
			}
			
			throw new ServiceException("Error accessing database", ex);
			
		}
		
		
		return isCreated;
	}

	/**
	 * method that creates a visitor
	 * @param visitor
	 * @return isLogin
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public boolean createVisitor(Visitor visitor) throws ServiceException {
		
		boolean isCreated = false;
		ManagerDAO manager = null;
		IVisitorDAO visitorDAO = null;
		
		try {
			manager = new ManagerDAO();
			visitorDAO = manager.getVisitorDAO();
			isCreated = visitorDAO.insertData(visitor);
			
			manager.closeConnection();
			
		} catch (DAOException ex){
			if(manager != null){
				manager.closeError();
			}
			
			throw new ServiceException("Error accessing database", ex);
			
		} 
		
		return isCreated;
	}
	/**
	 * method that creates a visitor
	 * @param visitor
	 * @return isCreated
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public boolean createVisitorV(Visitor visitor) throws ServiceException {
		
		boolean isCreated = false;
		ManagerDAO manager = null;
		IVisitorDAO visitorDAO = null;
		
		try {
			manager = new ManagerDAO();
			visitorDAO = manager.getVisitorDAO();
			isCreated = visitorDAO.insertDataV(visitor);
			
			manager.closeConnection();
			
		} catch (DAOException ex){
			if(manager != null){
				manager.closeError();
			}
			
			throw new ServiceException("Error accessing database", ex);
			
		} 
		
		return isCreated;
	}

	/**
	 * method that searches a visitor has been logged
	 * @param visitor
	 * @return locatedVisitor
	 * @throws ServiceException
	 */
	@Override
	public Visitor searchVisitor(Visitor visitor) throws ServiceException {
		
		ManagerDAO manager = null;
		IVisitorDAO visitorDAO = null;
		Visitor locatedVisitor = null;
		
		try {
		    manager = new ManagerDAO();
		    visitorDAO = manager.getVisitorDAO();
		    
		    locatedVisitor = visitorDAO.searchVisitor(visitor);

		    manager.closeConnection();
		    
		} catch(DAOException ex) {
		    if(manager != null) {
			manager.closeError();
			
		    }
		    
		    throw new ServiceException("Error accessing database", ex);
		    
		}

		return locatedVisitor;
	}
	
	
	/**
	 * method that searches a visitor has been logged
	 * @param people
	 * @return locatedPeople
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public People searchPeople(People people) throws ServiceException {
		
		ManagerDAO manager = null;
		IVisitorDAO visitorDAO = null;
		People locatedPeople = null;
		
		try {
		    manager = new ManagerDAO();
		    visitorDAO = manager.getVisitorDAO();
		    
		    locatedPeople = visitorDAO.searchPeople(people);

		    //add
		    manager.closeConnection();
		    
		} catch(DAOException ex) {
		    if(manager != null) {
			manager.closeError();
			
		    }
		    
		    throw new ServiceException("Error accessing database", ex);
		    
		}

		return locatedPeople;
	}

	/**
	 * method that shows the events which the visitor are registered in
	 * @param visitor
	 * @return events
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public List<Event> showRegisteredEventsForVisitor(Visitor visitor) throws ServiceException {
		
		ManagerDAO manager = null;
		EventSignUp eventSignUp = new EventSignUp();
		IEventSignUpDAO eventSignUpDAO = null;
		List<Event> events = new ArrayList<Event>();	

		try {
		    manager = new ManagerDAO();
		    eventSignUpDAO = manager.getEventSignUpDAO();
		    	     
		    eventSignUp.setIdVisitor(visitor.getVisitorId());
		    events = eventSignUpDAO.registeredEvents(eventSignUp);

		    manager.closeConnection();
		    
		} catch(DAOException ex) {
		    if(manager != null) {
			manager.closeError();
			
		    }
		    
		    throw new ServiceException("Error accessing database", ex);
		    
		}

		return events;
	}

	
	/**
	 * method that modifies the information of a visitor
	 * @param visitor
	 * @return affectedRows
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public int updateVisitor(Visitor visitor) throws ServiceException {
		ManagerDAO manager = null;
		IVisitorDAO visitorDAO = null;
		Integer affectedRows = 0;	

		try {
		    manager = new ManagerDAO();
		    visitorDAO = manager.getVisitorDAO();
		    
		    affectedRows = visitorDAO.updateVisitor(visitor);
		    
		} catch(DAOException ex) {
		    throw new ServiceException("Error accessing database", ex);
		    
		} finally {
		    if(manager != null) {
		    	if(affectedRows == 1) {
		    		manager.closeConnection();
			    
		    	} else {
		    		manager.closeError();
			    
		    	}		
		    }	    
		}

		return affectedRows;
	}

	
	/**
	 * method that modifies the information of a visitor
	 * @param people
	 * @return affectedRows
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public int updateVisitor(People people) throws ServiceException {
		ManagerDAO manager = null;
		IVisitorDAO visitorDAO = null;
		Integer affectedRows = 0;	

		try {
		    manager = new ManagerDAO();
		    visitorDAO = manager.getVisitorDAO();
		    
		    affectedRows = visitorDAO.updateVisitor(people);
		    
		} catch(DAOException ex) {
		    throw new ServiceException("Error accessing database", ex);
		    
		} finally {
		    if(manager != null) {
		    	if(affectedRows == 1) {
		    		manager.closeConnection();
			    
		    	} else {
		    		manager.closeError();
			    
		    	}		
		    }	    
		}

		return affectedRows;
	}
	
	
	/**
	 * method that modifies the DNI data of a visitor
	 * @param people
	 * @return affectedRows
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public int updateVisitorDni(People people) throws ServiceException {
		ManagerDAO manager = null;
		IVisitorDAO visitorDAO = null;
		Integer affectedRows = 0;	

		try {
		    manager = new ManagerDAO();
		    visitorDAO = manager.getVisitorDAO();
		    
		    affectedRows = visitorDAO.updateVisitorDni(people);
		    
		} catch(DAOException ex) {
		    throw new ServiceException("Error accessing database", ex);
		    
		} finally {
		    if(manager != null) {
		    	if(affectedRows == 1) {
		    		manager.closeConnection();
			    
		    	} else {
		    		manager.closeError();
			    
		    	}		
		    }	    
		}

		return affectedRows;
	}
	/**
	 * method that modifies the password information of a visitor
	 * @param visitor
	 * @return affectedRows
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
	public int changePassword(Visitor visitor) throws ServiceException {
		
		ManagerDAO manager = null;
		IVisitorDAO visitorDAO = null;
		Integer affectedRows = 0;	

		try {
		    manager = new ManagerDAO();
		    visitorDAO = manager.getVisitorDAO();
		    
		    affectedRows = visitorDAO.changePassword(visitor);
		    
		} catch(DAOException ex) {
		    throw new ServiceException("Error accessing database", ex);
		    
		} finally {
		    if(manager != null) {
			if(affectedRows == 1) {
			    manager.closeConnection();
			    
			} else {
			    manager.closeError();
			    
			}
		    }
		}

		return affectedRows;
	}
	
	/**
	 * method that implements a list of the events which the visitor are registered in
	 * @param visitor
	 * @return affectedRows
	 * @throws ServiceException
	 * @throws SQLException
	 */
	@Override
    public List<Event> showRegisteredEvents(Visitor visitor) throws ServiceException{
	
		ManagerDAO manager = null;
		EventSignUp eventSignUp = new EventSignUp();
		IEventSignUpDAO eventSignUpDAO = null;
		List<Event> events = new ArrayList<Event>();	
	
		try {
		    manager = new ManagerDAO();
		    eventSignUpDAO = manager.getEventSignUpDAO();
		    	     
		    eventSignUp.setIdVisitor(visitor.getVisitorId());
		    events = eventSignUpDAO.registeredEvents(eventSignUp);
	
		    manager.closeConnection();
		    
		} catch(DAOException ex) {
		    if(manager != null) {
			manager.closeError();
			
		    }
		    
		    throw new ServiceException("Error accessing database", ex);
		    
		}
	
		return events;
    }
	
}
